// components/calendar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  lifetimes: {
    attached: function () {
      let now = new Date();
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      this.data.nowDay = '' + year + '-' + month + '-' + now.getDate()
      this.data.nowYear = year
      this.data.nowMonth = month
      this.dateInit();
      this.setData({
        year: year,
        month: month,
        isToday: '' + year + '-' + month + '-' + now.getDate()
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    year: 0,
    month: 0,
    date: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0,
    nowDay: "",  // 今天的时间
    nowDayIndex: "",  // 今天的时间的索引,
    selsecDay: [],// 选中的时间集合
    nowYear: "",
    nowMonth: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toDate(e) {
      let date = e.currentTarget.dataset.date
      let clickYear = e.currentTarget.dataset.year
      // this.setData({
      //   isToday: date
      // })
      // 如果为选中过就添加的selsecDay
      let dateArr = this.data.dateArr
      let year = this.data.nowYear
      let month = this.data.nowMonth
      let nowDayIndex = this.data.nowDayIndex
      let selsecDay = this.data.selsecDay
      for(let i = 0;i<selsecDay.length;i++){
        let els = selsecDay[i]
        if(els.year == clickYear && els.isToday == date){
          selsecDay.splice(i,1)
          this.data.selsecDay = selsecDay
          dateArr.forEach((el,ins)=>{
            if(el.year == clickYear && el.isToday == date){
              el.is_select = false
              this.setData({
                dateArr:dateArr
              })
            }
          })
          return 
        }
      }
      console.log(this.data.selsecDay)
      console.log(dateArr)
      console.log(year)
      console.log(month)
      console.log(nowDayIndex)
      for (let i = 0; i < dateArr.length; i++) {
        // 判断年份大小
        // console.log(i)
        let el = dateArr[i];
        if (el.year < year) {  
          return console.log("选择年份较小")
        } else if (el.year == year && el.month < month) {
          return console.log("选择月份较小")
        } else if (el.year == year && el.month == month && date == el.isToday) {
          console.log(i)
          if(i >= nowDayIndex){
           this.add(el)
          }else{
            return console.log("选择大于当天")
          }
        } else if((el.year = year ||el.year > year) && el.month > month && date == el.isToday){
          this.add(el)
        }
      }
    },
    // 存储时间
    add(date){
      let is = true // 是否存入数据
      let selsecDay = this.data.selsecDay
      for (let i = 0; i < selsecDay.length; i++) {
        let el = selsecDay[i];
        if(el.isToday == date.isToday && el.year == date.year){ // 年份月份相同不可存入
          is = false
        }
        
      }
      if(is){
        this.data.selsecDay.push(date)
      }
      this.screen()
      console.log(this.data.selsecDay)
    },
    // 筛选选中的条件
    screen(){
      let selsecDay = this.data.selsecDay
      let dateArr = this.data.dateArr
      dateArr.forEach((el,i)=>{
        selsecDay.forEach((els,ins)=>{
          if(el.isToday == els.isToday && el.year == els.year){
            el.is_select = true
          }
        })
      })
      this.setData({
        dateArr:dateArr
      })
      console.log(dateArr)
    },
    dateInit: function (setYear, setMonth) {
      //全部时间的月份都是按0~11基准，显示月份才+1
      let dateArr = [];                        //需要遍历的日历数组数据
      let arrLen = 0;                            //dateArr的数组长度
      let now = setYear ? new Date(setYear, setMonth) : new Date();
      let year = setYear || now.getFullYear();
      let nextYear = 0;
      let month = setMonth || now.getMonth();                    //没有+1方便后面计算当月总天数
      let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
      let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay();                            //目标月1号对应的星期
      let dayNums = new Date(year, nextMonth, 0).getDate();                //获取目标月有多少天
      let obj = {};
      let num = 0;
      if (month + 1 > 11) {
        nextYear = year + 1;
        dayNums = new Date(nextYear, nextMonth, 0).getDate();
      }
      arrLen = startWeek + dayNums;
      for (let i = 0; i < arrLen; i++) {

        if (i >= startWeek) {
          num = i - startWeek + 1;
          obj = {
            isToday: '' + year + '-' + (month + 1) + '-' + num,
            dateNum: num,
            weight: 5,
            is_select: false,
            year: year,
            month: month + 1
          }
          if (this.data.nowDay == obj.isToday) {
            // obj.is_select = true
            this.data.nowDayIndex = i  // 记录当天的索引位置 用于判断选择时不选择逼当前月小的
          }
        } else {
          obj = {};
        }
        dateArr[i] = obj;
      }
      console.log(dateArr)
      this.setData({
        dateArr: dateArr
      })
      let nowDate = new Date();
      let nowYear = nowDate.getFullYear();
      let nowMonth = nowDate.getMonth() + 1;
      let nowWeek = nowDate.getDay();
      let getYear = setYear || nowYear;
      let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
      if (nowYear == getYear && nowMonth == getMonth) {
        this.setData({
          isTodayWeek: true,
          todayIndex: nowWeek
        })
      } else {
        this.setData({
          isTodayWeek: false,
          todayIndex: -1
        })
      }
    },
    lastMonth: function () {
      //全部时间的月份都是按0~11基准，显示月份才+1
      let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
      let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
      this.setData({
        year: year,
        month: (month + 1)
      })
      this.dateInit(year, month);
      this.screen()
    },
    nextMonth: function () {
      //全部时间的月份都是按0~11基准，显示月份才+1
      let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
      let month = this.data.month > 11 ? 0 : this.data.month;
      this.setData({
        year: year,
        month: (month + 1)
      })
      this.dateInit(year, month);
      this.screen()
    }
  }
})
